package com.baizhi.service;

import com.baizhi.dao.UserDao;
import com.baizhi.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class UserServiceImpl implements UserService{

    @Autowired
    private UserDao userDao;

    @Override
    public User login(User user) {
        User userDB = userDao.findByUsername(user.getUsername());
        if(userDB==null)
            throw new RuntimeException("提示：用户名不存在~~");
        if(!userDB.getPassword().equals(user.getPassword()))
            throw new RuntimeException("提示：用户名过密码不正确~~");
        return userDB;
    }

    @Override
    public int save(User user) {
        User userDB = userDao.findByUsername(user.getUsername());
        // 用戶存在
        if(userDB!=null){
            throw new RuntimeException("当前用户名已存在~~");
        }
        user.setStatus("已激活");
        user.setRegisterTime(new Date());
        return userDao.save(user);
    }

}
